﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.IDAL;

namespace KPIS.GERP.GHRMS.DAL
{
    public sealed class FieldGroupDAL : IFieldGroupDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public FieldGroupDAL()
        {

        }
        //public FieldPositionGroupsInfo GetFieldPositionGroupsInfo(Nullable<int> seq)
        //{
        //    FieldPositionGroupsInfo groupInfo = new FieldPositionGroupsInfo();
        //    string strSQL;

        //    try
        //    {
        //        dbManager.Open();
        //        dbManager.CreateParameters(2);

        //        dbManager.AddParameters(0, "@seq", seq);
        //        dbManager.AddParameters(1, "@DeleteRecord", SystemInfo._DeleteRecordStatus);

        //        strSQL = "SELECT FIELD_GROUP_SEQ, EMP_TYPE_SEQ, FIELD_GROUP_NAME, FIELD_GROUP_DESCR"
        //            + " FROM HRM_MP_FIELD_GROUPS"
        //            + " WHERE RECORD_STATUS not like @DeleteRecord"
        //                + " AND FIELD_GROUP_SEQ = @seq";
        //        IDataReader rdr = dbManager.ExecuteReader(CommandType.Text, strSQL);

        //        while (rdr.Read())
        //        {
        //            groupInfo.GroupSeq = rdr.IsDBNull(0) ? (int?)null : rdr.GetInt32(0);
        //            groupInfo.EmployeeTypeSeq = rdr.IsDBNull(1) ? (int?)null : rdr.GetInt32(1);
        //            groupInfo.GroupName = rdr.IsDBNull(2) ? "" : rdr.GetString(2);
        //            groupInfo.Description = rdr.IsDBNull(3) ? "" : rdr.GetString(3);
        //        }

        //        return groupInfo;
        //    }
        //    catch
        //    {
        //        return null;
        //        throw;
        //    }
        //    finally
        //    {
        //        dbManager.CloseReader();
        //        dbManager.Dispose();
        //    }
        //}
        public IList<FieldGroupInfo> ListFieldGroups(Nullable<int> Seq, Nullable<int> EmployeeTypes)
        {
            string strSQL;

            List<FieldGroupInfo> infoList = new List<FieldGroupInfo>();

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@Seq", Seq);
                dbManager.AddParameters(1, "@EmployeeTypes", EmployeeTypes);
                dbManager.AddParameters(2, "@DeleteRecord", SystemInfo._DeleteRecordStatus);

                strSQL = "SELECT FIELD_GROUP_SEQ, FIELD_GROUP_NAME, FIELD_GROUP_DESCR, EMP_TYPE_SEQ"
                        + ", CREATED_BY, UPDATED_BY"
                        + ", CONCAT(FIELD_GROUP_NAME, ' ', FIELD_GROUP_DESCR) AS FIELD_GROUP_NAME_DESCR"
                    + " FROM HRM_MP_FIELD_GROUPS"
                    + " WHERE RECORD_STATUS not like @DeleteRecord"
                    + (Seq.HasValue ? " AND FIELD_GROUP_SEQ = @Seq" : "")
                    + (EmployeeTypes.HasValue ? " AND EMP_TYPE_SEQ = @EmployeeTypes" : "");

                IDataReader rdr = dbManager.ExecuteReader(CommandType.Text, strSQL);

                while (rdr.Read())
                {
                    EmployeeTypeDAL empDAL = new EmployeeTypeDAL();
                    IList<EmployeeTypeInfo> empList = empDAL.ListEmployeeTypes(new EmployeeTypeInfo(rdr.IsDBNull(3) ? 0 : rdr.GetInt32(3)));
                    
                    FieldGroupInfo Info = new FieldGroupInfo(rdr.GetInt32(0)
                            , rdr.IsDBNull(1) ? "" : rdr.GetString(1)
                            , rdr.IsDBNull(2) ? "" : rdr.GetString(2)
                            , new EmployeeTypeInfo(empList[0].Sequence, empList[0].Name, "", null, null)
                            , rdr.IsDBNull(4) ? (int?)null : rdr.GetInt32(4)
                            , rdr.IsDBNull(5) ? (int?)null : rdr.GetInt32(5));
                    Info.Title = rdr.IsDBNull(6) ? "" : rdr.GetString(6);
                    infoList.Add(Info);
                  }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
            return infoList;
        }

        public void Insert(FieldGroupInfo Info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(6);

                dbManager.AddParameters(0, "@FieldGroupName", Info.Name);
                dbManager.AddParameters(1, "@FieldGroupDescr", Info.Description);
                dbManager.AddParameters(2, "@EmpTypeSeq", Info.EmployeeTypes.Sequence);
                dbManager.AddParameters(3, "@RecordStatus", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(4, "@CreatedBy", Info.CreatedBy);
                dbManager.AddParameters(5, "@CreatedWhen", DateTime.Today);

                strSQL = "INSERT INTO HRM_MP_FIELD_GROUPS(FIELD_GROUP_NAME, FIELD_GROUP_DESCR, EMP_TYPE_SEQ"
                        + ", RECORD_STATUS, CREATED_BY, CREATED_WHEN)"
                    + " VALUES(@FieldGroupName, @FieldGroupDescr, @EmpTypeSeq, @RecordStatus, @CreatedBy, @CreatedWhen)";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Update(FieldGroupInfo Info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(6);

                dbManager.AddParameters(0, "@Seq", Info.Sequence);
                dbManager.AddParameters(1, "@FieldGroupName", Info.Name);
                dbManager.AddParameters(2, "@FieldGroupDescr", Info.Description);
                dbManager.AddParameters(3, "@EmpTypeSeq", Info.EmployeeTypes.Sequence);
                dbManager.AddParameters(4, "@UpdatedBy", Info.UpdatedBy);
                dbManager.AddParameters(5, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE HRM_MP_FIELD_GROUPS"
                           + " SET FIELD_GROUP_NAME = @FieldGroupName"
                                + ", FIELD_GROUP_DESCR = @FieldGroupDescr"
                                + ", EMP_TYPE_SEQ = @EmpTypeSeq"
                                + ", UPDATED_BY = @UpdatedBy"
                                + ", UPDATED_WHEN = @UpdatedWhen"
                           + " WHERE FIELD_GROUP_SEQ = @Seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Delete(int Sequence, int UpdatedBy)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@seq", Sequence);
                dbManager.AddParameters(1, "@record_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@updated_by", UpdatedBy);
                dbManager.AddParameters(3, "@updated_when", DateTime.Today);

                strSQL = "UPDATE HRM_MP_FIELD_GROUPS"
                    + " SET RECORD_STATUS = @record_status"
                        + ", UPDATED_BY = @updated_by"
                        + ", UPDATED_WHEN = @updated_when"
                    + " WHERE FIELD_GROUP_SEQ = @seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

    }
}
